package com.heima.wemedia.controller.v1;

import com.heima.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.NewsDto;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmNewsService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController {

    @Resource
    private WmNewsService wmNewsService;

    /**
     *  分页查询所有文章
     * @param dto 分页条件信息
     * @return
     */
    @PostMapping("/list")
    public ResponseResult<List<WmNews>> findAll(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findAll(dto);
    }

    /**
     *  上传文章
     * @param dto 上传的文章信息
     * @return
     */
    @PostMapping("/submit")
    public ResponseResult<Object> submitNews(@RequestBody WmNewsDto dto) {
        return wmNewsService.submitNews(dto);
    }

    /**
     *  根据id查询文章详情
     * @param id 文章id
     * @return
     */
    @GetMapping("/one/{id}")
    public ResponseResult<WmNews> selectDetail(@PathVariable("id")Integer id){
        return ResponseResult.okResult(wmNewsService.getById(id));
    }

    /**
     *  根据id删除文章信息
     * @param id 文章id
     * @return
     */
    @GetMapping("del_news/{id}")
    public ResponseResult<Object> delNews(@PathVariable("id") Integer id){
        return wmNewsService.delNews(id);
    }

    /**
     *   上架或下架文章
     * @param newsDto
     * @return
     */
    @PostMapping("down_or_up")
    public ResponseResult<Object> DownOrUpNews(@RequestBody NewsDto newsDto){
        return wmNewsService.DownOrUpNews(newsDto);
    }

}
